﻿var mytype;
// 注意，必须设置了run_at=document_start 此段代码才会生效 通过页面加载完成判断增加按钮
// 这里只有页面刷新才会调用
document.addEventListener('DOMContentLoaded', function()
{
	console.log('DOMContentLoaded')
	if (location.host == 'localhost:5173' )//if(location.host == 'www.baidu.com') 
	{
		console.log('初次进入脚本设置页面')
        // 再对应页面注入自定义JS
		mytype=setInterval(injectCustomJs, 2000);
	}	
});

// 监听hash路由改变调用方法
window.addEventListener('hashchange',function(event){
	// if (location.href.indexOf('detail')!= -1 )//if(location.host == 'www.baidu.com') 
	if (location.host == 'localhost:5173')//if(location.host == 'www.baidu.com') 
	{
		console.log('页面改变进入脚本设置页面')
        // 再对应页面注入自定义JS
		mytype=setInterval(injectCustomJs, 2000);
	}	
})


// 向页面注入JS
function injectCustomJs(jsPath)
{

	jsPath = jsPath || 'js/inject.js';
	var temp = document.createElement('script');
	temp.setAttribute('type', 'text/javascript');
	// 获得的地址类似：chrome-extension://ihcokhadfjfchaeagdoclpnjdiokfakg/js/inject.js
	temp.src = chrome.extension.getURL(jsPath);
	
	temp.onload = function()
	{

		var account= document.getElementById('form_item_username');
		if(account.value){
			return null
		}
		
    var inputEvent = new Event('input', { bubbles: true });
		account.value='admin';
		account.dispatchEvent(inputEvent);
		

		var password= document.getElementById('form_item_password');
		password.value='1222555555'
		password.dispatchEvent(inputEvent);

		console.log('账号',account.value);
		console.log('密码',password.value);

		// 模拟点击提交按钮
		var submit=document.querySelector("button");
		if(document.all){
			submit.click();
		}else{
			//  其它浏览器
			let event = new Event('click', {"bubbles":true, "cancelable":true});
			submit.dispatchEvent(event);
		}
		
	};
	document.body.appendChild(temp);
}